package com.intercom.fdfr;

import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


/**
 * Created by Dushyant on 2/1/2017.
 */

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder surfaceHolder;
    private CameraController cameraController;
    private final String TAG = "CameraPreview";

    public CameraPreview(Context context, CameraController cameraController) {
        super(context);
        Log.i(TAG, "CameraPreview: ");
        this.cameraController = cameraController;
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i(TAG, "surfaceCreated: ");
        cameraController.setPreviewDisplay(holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG, " surfaceChanged" + ": width " + width + ", height " + height);
        if (surfaceHolder.getSurface() == null) {
            return;
        } else {
            System.out.println("Width Surface Changed");
            cameraController.stopPreview();
            cameraController.startPreview();

        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i(TAG, "surfaceDestroyed: ");
        cameraController.stopPreview();
    }

}
